package com.eascs.web.o2o.scheduler.quartz;

import java.util.List;

import javax.inject.Inject;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StopWatch;
import com.eascs.web.o2o.quartz.util.BaseJobUtil;
import com.eascs.web.o2o.quartz.util.ExecutorService;
import com.eascs.web.o2o.scheduler.service.SynchroWorkflow;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.entity.wf.ProcessTaskNode;
import com.eascs.workflow.common.service.timeEffective.AgingDubboService;
import com.eascs.workflow.common.service.timeEffective.TimeffectiveDubboService;
import com.eascs.workflow.common.service.wf.WorkFlowService;
import com.eascs.workflow.common.vo.ModelResult;

/**
 * workflow：与workflow相关的定时任务，并且数据落库在workflow中
 */
@Component("WorkFlowSynchroQuartz")
public class WorkFlowSynchroQuartz extends BaseJobUtil {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Reference
	WorkFlowService workFlowService;

	@Reference
	TimeffectiveDubboService timeffective;

	@Reference
	AgingDubboService agingDubboService;

	@Inject
	SynchroWorkflow synchroWorkflow;

	public void wfWorkWarnEmail() {
		executor("WorkFlowSynchroQuartz", "wfWorkWarnEmail", "workflow 待办提醒任务", new ExecutorService() {
			@Override
			public void exe() {
				StopWatch stopWatch = new StopWatch();
				ModelResult<Boolean> result = workFlowService.wfWorkWarnEmail();
				long stop = stopWatch.elapsedTime() / 60000;
				if (result.isSuccess()) {
					boolean flag = result.getModel();
					if (flag) {
						logger.info("[workflow] [待办提醒]任务[完成]，共耗时：[{}]分", stop);
					} else {
						logger.warn("[workflow] [待办提醒]任务[失败]，共耗时：[{}]分", stop);
					}
				} else {
					logger.warn("[workflow] [待办提醒]任务[异常]，错误信息[{}],共耗时：[{}]分", result.getErrMsg(), stop);
				}

				logger.info("[workflow] [待办提醒]任务[结束]......");
			}
		});
	}

	public void timeffective() {
		executor("WorkFlowSynchroQuartz", "timeffective", "时效管理0", new ExecutorService() {
			@Override
			public void exe() {
				int pageSize = 50;
				int pageIndex = 0;
				while (pageIndex >= 0) {
					ModelResult<List<ProcessTaskNode>> result = timeffective.queryProcessNodeByPage(pageSize,
							pageIndex);
					if (result.isSuccess()) {
						List<ProcessTaskNode> tasks = result.getModel();
						if (CollectionUtils.isEmpty(tasks)) {
							break;
						}
						logger.info("[workflow] [时效管理零点]任务第[{}]页[ProcessTaskNode]共有数据[{}]条", pageIndex, tasks.size());
						for (ProcessTaskNode task : tasks) {
							ModelResult<Boolean> result2 = timeffective.timeffectiveNodeTask(task);
							if (!result2.isSuccess()) {
								logger.error(
										"[workflow] [时效管理零点]任务[错误][timeffective.timeffectiveTask]......pageIndex:[{}]",
										pageIndex);
							}
						}
						pageIndex++;
						if (tasks.size() < pageSize) {
							pageIndex = -1;
						}
					} else {
						logger.error("[workflow] [时效管理零点]任务[错误]......pageIndex:[{}],ErrorMsg:[{}]", result.getErrMsg(),
								pageIndex);
					}
				}
			}
		});
	}

	public void timeffective2() {
		executor("WorkFlowSynchroQuartz", "timeffective2", "时效管理2点", new ExecutorService() {
			@Override
			public void exe() {
				int pageSize = 50;
				int pageIndex = 0;
				while (pageIndex >= 0) {
					ModelResult<List<ProcessTaskMain>> result = timeffective.queryProcessMainByPage(pageSize,
							pageIndex);
					if (result.isSuccess()) {
						List<ProcessTaskMain> tasks = result.getModel();
						if (CollectionUtils.isEmpty(tasks)) {
							break;
						}
						logger.info("[workflow] [时效管理2点]任务第[{}]页[ProcessTaskMain]共有数据[{}]条", pageIndex, tasks.size());
						for (ProcessTaskMain task : tasks) {
							ModelResult<Boolean> result2 = timeffective.timeffectiveMainTask(task);
							if (!result2.isSuccess()) {
								logger.error(
										"[workflow] [时效管理2点]任务[错误][timeffective.timeffectiveMainTask]......pageIndex:[{}]",
										pageIndex);
							}
						}
						pageIndex++;
						if (tasks.size() < pageSize) {
							pageIndex = -1;
						}
					} else {
						logger.error("[workflow] [时效管理2点]任务[错误]......pageIndex:[{}]ErrorMsg:[{}]", result.getErrMsg(),
								pageIndex);
					}
				}
			}
		});
	}

	public void timeffective3() {
		executor("WorkFlowSynchroQuartz", "timeffective3", "时效管理  3", new ExecutorService() {
			@Override
			public void exe() {
				agingDubboService.insertLoanapply();
			}
		});
	}

	public void timeffective4() {
		executor("WorkFlowSynchroQuartz", "timeffective4", "时效管理  4", new ExecutorService() {
			@Override
			public void exe() {
				agingDubboService.insertWorkFlow();
			}
		});
	}

	public void timeffective5() {
		executor("WorkFlowSynchroQuartz", "timeffective5", "时效管理  5", new ExecutorService() {
			@Override
			public void exe() {
				agingDubboService.insertNode();
			}
		});
	}

	public void timeffective6() {
		executor("WorkFlowSynchroQuartz", "timeffective6", "时效导出", new ExecutorService() {
			@Override
			public void exe() {
				agingDubboService.syncAgingExport();
			}
		});
	}

	public void saveWorkflowJob() {
		executor("WorkFlowSynchroQuartz", "saveWorkflowJob", "将oa中的基础数据同步到workflow", new ExecutorService() {
			@Override
			public void exe() {
				synchroWorkflow.saveData();
			}
		});
	}

}
